package com.example.servicedemo;

import android.annotation.SuppressLint;
import java.util.HashMap;
import java.util.Map;

/***********************************************************************
 * Enum container that holds status codes and string values that
 * specifies the conditions of the error in user readable format.
 * 
 * @author Tomas Rivera
 * 
 ***********************************************************************/
public enum BtleDeviceStatus {
	/**********************************************************************
	 * BtleDevice connected and ready to transmit sensor data.
	 **********************************************************************/
	BTLE_STATUS_OK(0),
	/**********************************************************************
	 * Unable to access Android Bluetooth Service.
	 **********************************************************************/
	BTLE_ANDROID_BLUETOOTH_SERVICE_UNAVAILABLE(1),
	/**********************************************************************
	 * Bluetooth LE is not available on this device.
	 **********************************************************************/
	BTLE_BLUETOOTH_LE_NOT_AVAILABLE(2),
	/**********************************************************************
	 * Bluetooth radio is disabled on device.
	 **********************************************************************/
	BTLE_BLUETOOTH_RADIO_DISABLED(3),
	/**********************************************************************
	 * Bluetooth address is invalid.
	 **********************************************************************/
	BTLE_ADDRESS_INVALID(4),
	/**********************************************************************
	 * Unable to contact device, likely powered off or out of range.
	 **********************************************************************/
	BTLE_DEVICE_CONNECTION_FAILED(5),
	/**********************************************************************
	 * Unable to find any valid services advertised on device.
	 **********************************************************************/
	BTLE_SERVICE_DISCOVERY_FAILED(6),
	/**********************************************************************
	 * Device does not have a valid service that we support.
	 **********************************************************************/
	BTLE_SERVICE_NOT_FOUND(7),
	/**********************************************************************
	 * Could not connect to target service.
	 **********************************************************************/
	BTLE_SERVICE_CONNECTION_FAILED(8),
	/**********************************************************************
	 * Unable to find requested measurement characteristic.
	 **********************************************************************/
	BTLE_CHARACTERISTIC_NOT_FOUND(9),
	/**********************************************************************
	 * Failed to set characteristic notification for target characteristic.
	 **********************************************************************/
	BTLE_CHARACTERISTIC_SET_FAILED(10),
	/**********************************************************************
	 * Failed to acquire requested characteristic descriptor, notifications
	 * are not enabled.
	 **********************************************************************/
	BTLE_CHARACTERISTIC_NOTIFY_START_FAILED(11),
	/**********************************************************************
	 * onLeScan() timeout.
	 **********************************************************************/
	BTLE_DEVICE_DISCOVERY_TIMEOUT(12),
	/**********************************************************************
	 * Timeout while waiting to get characteristic from device.
	 **********************************************************************/
	BTLE_DEVICE_GET_CHARACTERISTIC_TIMEOUT(13),
	/**********************************************************************
	 * BluetoothGatt callback informed us that the connection was lost.
	 **********************************************************************/
	BTLE_DEVICE_CONNECTION_DROPPED(14),
	/**********************************************************************
	 * No device exists for the address specified by the client.
	 **********************************************************************/
	BTLE_DEVICE_DOES_NOT_EXIST(15),
	
	/**********************************************************************
	 * Undefined error.
	 **********************************************************************/
	BTLE_UNDEFINED_ERROR(100);
	
	private int value;
	
	private BtleDeviceStatus(int v) {
		value = v;
	}
	
	/**************************************************************
	 * Retrieves integer value of the enum.
	 * @returns
	 * 	BtleDeviceStatus numeric value.
	 **************************************************************/
	public int getValue() {
		return value;
	}
	
	@SuppressLint("UseSparseArrays")
	private static final Map<Integer, BtleDeviceStatus> intToStatusMap = new HashMap<Integer, BtleDeviceStatus>();
	static {
		for(BtleDeviceStatus status : BtleDeviceStatus.values()) {
			intToStatusMap.put(status.value, status);
		}
	}
	
	/**************************************************************
	 * Converts integer to BtleDeviceStatus enum object.
	 * @param	value
	 * 	Integer value to be translated to BtleDeviceStatus.
	 * @return	
	 * 	BtleDeviceStatus enum object that corresponds to integer
	 *  value.
	 **************************************************************/
	public static BtleDeviceStatus fromInt(int value) {
		BtleDeviceStatus status = intToStatusMap.get(Integer.valueOf(value));
		if(status == null) {
			return BtleDeviceStatus.BTLE_UNDEFINED_ERROR;
		}
		
		return status;
	}
	
	/**************************************************************
	 * Returns a string that explains the error.
	 * @return	User readable string that gives details of the error.
	 **************************************************************/
	@Override
	public String toString() {
		switch(this) {
		case BTLE_STATUS_OK:
			return new String("BtleDevice connected and ready to transmit sensor data.");
		case BTLE_ANDROID_BLUETOOTH_SERVICE_UNAVAILABLE:
			return new String("Unable to access Android Bluetooth Service.");
		case BTLE_BLUETOOTH_LE_NOT_AVAILABLE:
			return new String("Bluetooth LE is not available on this device.");
		case BTLE_BLUETOOTH_RADIO_DISABLED:
			return new String("Bluetooth radio is disabled on device.");
		case BTLE_ADDRESS_INVALID:
			return new String("Bluetooth address is invalid.");
		case BTLE_DEVICE_CONNECTION_FAILED:
			return new String("Unable to contact device, likely powered off or out of range.");
		case BTLE_SERVICE_DISCOVERY_FAILED:
			return new String("Unable to find any valid services advertised on device.");
		case BTLE_SERVICE_NOT_FOUND:
			return new String("Device does not have a valid service that we support.");
		case BTLE_SERVICE_CONNECTION_FAILED:
			return new String("Could not connect to target service.");
		case BTLE_CHARACTERISTIC_NOT_FOUND:
			return new String("Unable to find requested measurement characteristic.");
		case BTLE_CHARACTERISTIC_SET_FAILED:
			return new String("Failed to set characteristic notification for target characteristic.");
		case BTLE_CHARACTERISTIC_NOTIFY_START_FAILED:
			return new String("Failed to acquire requested characteristic descriptor, notifications are not enabled.");
		case BTLE_DEVICE_CONNECTION_DROPPED:
			return new String("BtleDevice connection dropped.");
		case BTLE_DEVICE_DOES_NOT_EXIST:
			return new String("No BtleDevice exists for specified address.");
		case BTLE_UNDEFINED_ERROR:
		default:		
			return new String("Undefined error.");
		}
		
	}
	
}
